package plutuscat.shishicai.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* 三星号码
* @author dw_chenqi
* @date 2016年5月30日上午12:07:15
*/
public class Sanxing {

	private int first;
	private int second;
	private int third;
	private String number; //组合而成的号码
	private String sortedNumber; // 数字排序之后的号码

	public Sanxing() {
	}

	public Sanxing(int first, int second, int third) {
		this.first = first;
		this.second = second;
		this.third = third;
		this.number = getNumber();
		this.sortedNumber = getSortedNumber();
	}

	public String getNumber() {
		return String.valueOf(first) + second + third;
	}

	public String getSortedNumber() {
		//TODO:TreeSet排序会去重,只排序,不去重,不满足需求

		StringBuilder sb = new StringBuilder(3);
		if (second <= first) {
			sb.append(second).append(first);
		} else {
			sb.append(first).append(second);
		}
		int min = Integer.parseInt(Character.toString(sb.charAt(0)));
		int max = Integer.parseInt(Character.toString(sb.charAt(1)));
		if (third <= min) {
			sb.insert(0, third);
		} else if (third >= max) {
			sb.insert(2, third);
		} else {
			sb.insert(1, third);
		}

		return sb.toString();
	}

	public static List<Sanxing> getAll() {
		List<Sanxing> all = new ArrayList<Sanxing>(1000);
		for (int i = 0; i < 10; i++) {

			for (int j = 0; j < 10; j++) {

				for (int z = 0; z < 10; z++) {
					all.add(new Sanxing(i, j, z));
				}
			}

		}
		return all;
	}

	public Set<String> getAllBaozi() {
		Set<String> set = new HashSet<String>();
		for (Sanxing sanxing : getAll()) {
			if (sanxing.isBaozi()) {
				set.add(sanxing.getSortedNumber());
			}
		}
		return set;
	}

	/**
	 * 获取所有组三组合 
	 * @return
	 */
	public Set<String> getAllGroup3() {
		Set<String> set = new HashSet<String>();
		List<Sanxing> sanxingList = Sanxing.getAll();
		for (Sanxing sanxing : sanxingList) {
			if (sanxing.isGroup3()) {
				set.add(sanxing.getSortedNumber());
			}
		}
		return set;
	}

	/**
	 * 获取所有组六组合
	 * @return
	 */
	public Set<String> getAllGroup6() {
		Set<String> set = new HashSet<String>();
		List<Sanxing> sanxingList = Sanxing.getAll();
		for (Sanxing sanxing : sanxingList) {
			if (sanxing.isGroup6()) {
				set.add(sanxing.getSortedNumber());
			}
		}
		return set;
	}

	private boolean isBaozi() {
		return this.first == this.second && this.second == this.third;
	}

	/**
	 * 是否为组三
	 * @return
	 */
	private boolean isGroup3() {
		if (this.isBaozi()) {
			return false;
		}
		return first == second || second == third || first == third;
	}

	/**
	 * 组六
	 * @return
	 */
	private boolean isGroup6() {
		if (this.isGroup3()) {
			return false;
		}
		return first != second && second != third;
	}
}
